import { defineStore } from 'pinia'

interface User {
  id: number
  name: string
  skill: string
}

interface UserState {
  users: User[]
  valid: boolean
}

export const useUserStore = defineStore('user', {
  state: (): UserState => {
    return {
      users: [],
      valid: false
    }
  },

  getters: {
    getUserInfo(): User | null {
      return this.users.length > 0 ? this.users[0] ?? null : null
    },
    invalid(): boolean {
      return !this.valid
    }
  },

  actions: {
    addUser(user: Omit<User, 'id'>) {
      const newUser: User = {
        id: Date.now(),
        ...user
      }
      this.users.push(newUser)
    },

    deleteUser(id: number) {
      const index = this.users.findIndex(user => user.id === id)
      if (index > -1) {
        this.users.splice(index, 1)
      }
    },

    updateUser(id: number, userData: Partial<Omit<User, 'id'>>) {
      const user = this.users.find(u => u.id === id)
      if (user) {
        Object.assign(user, userData)
      }
    },
  }
})

